package com.fc.campusapplicationsystem.service;

import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kyc
 * @date 2020/5/25 13:52
 */
@Service
public class MailServiceImpl implements MailService {


    /**
     * 发送邮件
     * @param account       登录账户
     * @param password      登陆密码
     * @param domain        域
     * @param emailAddress  登录账户对应的邮件地址
     * @param subject       标题
     * @param recipientTo   收件人
     * @param recipientCc   抄送
     * @param recipientBcc  密抄
     * @param attachments   附件
     * @param message       邮件消息内容
     * @return              boolean
     */
    @Override
    public void sendMail(String account, String password, String domain, String emailAddress, String subject,
                         List<String> recipientTo, String message) {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        ExchangeCredentials credentials = new WebCredentials("ky001596","001596@chery","chery");
//        service.setCredentials(credentials);
//        service.autodiscoverUrl("kuaiyunchuan@mychery.com");

//        service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
//        credentials = new WebCredentials(account, password, domain);
        service.setCredentials(credentials);
        try {
            service.autodiscoverUrl(emailAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EmailMessage msg;
        try {
            msg = new EmailMessage(service);
            // 设置邮件标题
            msg.setSubject(subject);
            // 邮件内容
            MessageBody body = MessageBody.getMessageBodyFromText(message);
            body.setBodyType(BodyType.HTML);
            msg.setBody(body);
            // 收件人
            if (!recipientTo.isEmpty()) {
                for (String to : recipientTo) {
                    msg.getToRecipients().add(to);
                }
            }
            /*// 抄送
            if (!recipientCc.isEmpty()) {
                for (String cc : recipientCc) {
                    msg.getCcRecipients().add(cc);
                }
            }
            // 密抄
            if (!recipientBcc.isEmpty()) {
                for (String bcc : recipientBcc) {
                    msg.getBccRecipients().add(bcc);
                }
            }
            // 附件
            if (!attachments.isEmpty()) {
                for(String att : attachments) {
                    msg.getAttachments().addFileAttachment(att);
                }
            }*/
            msg.send();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("邮件发送失败");
        }
    }


}
